/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.service.activity;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.shop.entity.activity.HkShopDiscount;
import com.thinkgem.jeesite.modules.hk.shop.dao.activity.HkShopDiscountDao;
import com.thinkgem.jeesite.modules.hk.shop.entity.activity.HkShopDiscountProduct;
import com.thinkgem.jeesite.modules.hk.shop.dao.activity.HkShopDiscountProductDao;

/**
 * 乐购折扣专场Service
 * @author 罗天文
 * @version 2018-05-16
 */
@Service
@Transactional(readOnly = true)
public class HkShopDiscountService extends CrudService<HkShopDiscountDao, HkShopDiscount> {

	@Autowired
	private HkShopDiscountProductDao hkShopDiscountProductDao;
	
	public HkShopDiscount get(String id) {
		HkShopDiscount hkShopDiscount = super.get(id);
		hkShopDiscount.setHkShopDiscountProductList(hkShopDiscountProductDao.findList(new HkShopDiscountProduct(hkShopDiscount)));
		return hkShopDiscount;
	}
	
	public List<HkShopDiscount> findList(HkShopDiscount hkShopDiscount) {
		return super.findList(hkShopDiscount);
	}
	
	public Page<HkShopDiscount> findPage(Page<HkShopDiscount> page, HkShopDiscount hkShopDiscount) {
		return super.findPage(page, hkShopDiscount);
	}
	
	@Transactional(readOnly = false)
	public void save(HkShopDiscount hkShopDiscount) {
		super.save(hkShopDiscount);
		for (HkShopDiscountProduct hkShopDiscountProduct : hkShopDiscount.getHkShopDiscountProductList()){
			if (hkShopDiscountProduct.getId() == null){
				continue;
			}
			hkShopDiscountProduct.setActivityType("3");
			hkShopDiscountProduct.setBeginTime(hkShopDiscount.getBeginTime());
			hkShopDiscountProduct.setEndTime(hkShopDiscount.getEndTime());
			if (HkShopDiscountProduct.DEL_FLAG_NORMAL.equals(hkShopDiscountProduct.getDelFlag())){
				if (StringUtils.isBlank(hkShopDiscountProduct.getId())){
					hkShopDiscountProduct.setProductDiscount(hkShopDiscount);
					hkShopDiscountProduct.preInsert();
					hkShopDiscountProductDao.insert(hkShopDiscountProduct);
				}else{
					hkShopDiscountProduct.preUpdate();
					hkShopDiscountProductDao.update(hkShopDiscountProduct);
				}
			}else{
				hkShopDiscountProductDao.delete(hkShopDiscountProduct);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkShopDiscount hkShopDiscount) {
		super.delete(hkShopDiscount);
		hkShopDiscountProductDao.delete(new HkShopDiscountProduct(hkShopDiscount));
	}
	
}